package com.zhifei.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.DispatcherUtils;
import com.zhifei.common.utils.Result;
import com.zhifei.system.domain.DingUserEntity;
import com.zhifei.system.domain.PersonProjectEntity;
import com.zhifei.system.domain.vo.PersonProjectUserVo;
import com.zhifei.system.domain.vo.PersonProjectVo;
import com.zhifei.system.mapper.PersonProjectMapper;
import com.zhifei.system.service.DingUserService;
import com.zhifei.system.service.PersonProjectService;
import com.zhifei.system.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PersonProjectServiceImpl extends ServiceImpl<PersonProjectMapper, PersonProjectEntity> implements PersonProjectService {

    @Autowired
    private DingUserService dingUserService;
    @Autowired
    private PersonProjectMapper personProjectMapper;

    @Override
    public Result calc() {
        List<PersonProjectEntity> personProjectEntityList = this.lambdaQuery().list();
        List<String> deptIdList = personProjectEntityList.stream().map(PersonProjectEntity::getDeptId).collect(Collectors.toList());
        List<DingUserEntity> dingUserEntityList = dingUserService.lambdaQuery().in(DingUserEntity::getDeptId,deptIdList).list();
        Map<String, List<DingUserEntity>> dingUserEntityListMap =  dingUserEntityList.stream().collect(Collectors.groupingBy(DingUserEntity::getDeptId));
        for(PersonProjectEntity personProjectEntity : personProjectEntityList){
            for (String deptId : dingUserEntityListMap.keySet()) {
                if(deptId.equals(personProjectEntity.getDeptId())){
                    List<DingUserEntity> dingUserEntities = dingUserEntityListMap.get(deptId);
                    BigDecimal recruitCommission = BigDecimal.ZERO;
                    for(DingUserEntity dingUserEntity : dingUserEntities){
                        String userRank = dingUserEntity.getUserRank();
                        String userPost = dingUserEntity.getUserPost();
                        if(!userPost.equals("客户代表")){
                            if(userRank.equals("初级")){
                                recruitCommission = recruitCommission.add(new BigDecimal(250));
                            }else if(userRank.equals("初中级") || userRank.equals("中级")){
                                recruitCommission = recruitCommission.add(new BigDecimal(350));
                            }else if(userRank.equals("中高级") || userRank.equals("高级")){
                                recruitCommission = recruitCommission.add(new BigDecimal(450));
                            }
                        }
                    }
                    personProjectEntity.setRecruitCommission(recruitCommission);
                    BigDecimal netProfit = personProjectEntity.getNetProfit().subtract(recruitCommission);//纯利（毛利-公司管理成本-风险储备金-差旅费用-市场营销补贴-财务费用-销售提成-招聘提成）
                    BigDecimal netProfitRate = netProfit.divide(personProjectEntity.getContractAmount(),2, RoundingMode.HALF_UP);;//纯利率（纯利/合同金额）
                    personProjectEntity.setNetProfit(netProfit);
                    personProjectEntity.setNetProfitRate(netProfitRate);
                }
            }
        }
        this.updateBatchById(personProjectEntityList);
        return Result.success();
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        String customerName = (String) params.get("customerName");
        String projectName = (String) params.get("projectName");
        String orderNo = (String) params.get("orderNo");
        String orderStartDate = (String) params.get("orderStartDate");
        String orderEndDate = (String) params.get("orderEndDate");
        List<PersonProjectVo> personProjectVoList = personProjectMapper.queryPage(page,customerName,projectName,orderNo,orderStartDate,orderEndDate);
        page.setRecords(personProjectVoList);
        return new PageUtils(page);
    }

    @Override
    public PageUtils pageUser(Map<String, Object> params) {
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        String deptId = (String) params.get("deptId");
        Integer userStatus = DispatcherUtils.parseInteger(params.get("userStatus"));
        List<PersonProjectUserVo> personProjectUserVoList = personProjectMapper.pageUser(page,deptId,userStatus);
        page.setRecords(personProjectUserVoList);
        return new PageUtils(page);
    }

}
